/**
 * 
 */
package com.yidu.drug.service.impl;

import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.reflect.TypeToken;
import com.yidu.drug.common.Page;
import com.yidu.drug.common.Tools;
import com.yidu.drug.dao.MaterialsModelMapper;
import com.yidu.drug.dao.PoBreakdownMapper;
import com.yidu.drug.dao.PurchaseOrderModelMapper;
import com.yidu.drug.model.MaterialsModel;
import com.yidu.drug.model.PoBreakdown;
import com.yidu.drug.model.PurchaseOrderModel;
import com.yidu.drug.service.PoBreakdownService;

/**
 * 总店采购订单明细表Service实现类
 * @author 王志伟
 * @date 2018年8月9日 下午8:07:02
 * @version 1.0
 */
@Service
public class PoBreakdownServiceImpl implements PoBreakdownService{
	
	//总店采购订单明细Dao
	@Resource
	private PoBreakdownMapper poBreakdownDao;
	
	//材料DAO
	@Resource
	private MaterialsModelMapper matDao;
	
	//采购订单表
	@Resource
	private PurchaseOrderModelMapper poDao;
	/**
	 * 根据采购订单明细ID删除
	 */
	@Override
	public int deleteByPrimaryKey(String pobId) {
		// TODO Auto-generated method stub
		return poBreakdownDao.deleteByPrimaryKey(pobId);
	}

	/**
	 * 增加采购订单明细所有信息
	 */
	@Override
	public int insert(PoBreakdown record) {
		// TODO Auto-generated method stub
		return poBreakdownDao.insert(record);
	}

	/**
	 * 增加采购订单明细部分信息
	 */
	@Override
	public int insertSelective(PoBreakdown record) {
		// TODO Auto-generated method stub
		return poBreakdownDao.insertSelective(record);
	}

	/**
	 * 根据采购订单明细ID查询采购订单明细所有
	 */
	@Override
	public PoBreakdown selectByPrimaryKey(String pobId) {
		// TODO Auto-generated method stub
		return poBreakdownDao.selectByPrimaryKey(pobId);
	}

	/**
	 * 根据采购订单明细ID修改部分信息
	 */
	@Override
	public int updateByPrimaryKeySelective(PoBreakdown record) {
		// TODO Auto-generated method stub
		return poBreakdownDao.updateByPrimaryKeySelective(record);
	}
	
	/**
	 * 根据采购订单明细ID修改所有信息
	 */
	@Override
	public int updateByPrimaryKey(PoBreakdown record) {
		// TODO Auto-generated method stub
		return poBreakdownDao.updateByPrimaryKey(record);
	}

	@Override
	public Map<String, Object> findAll(String limit, String page, String pobName,String poId) {
		//NEW一个采购明细的模型,加入页面信息和名称
		PoBreakdown poBreakdown = new PoBreakdown();
		Page pa=new Page();
		pa.setPage(Integer.valueOf(page));
		pa.setRows(Integer.valueOf(limit));
		poBreakdown.setPage(pa);
		poBreakdown.setPobName(pobName);
		if(!poId.equals("null") ){
			poBreakdown.setPoId(poId);
		}
		if(poId.equals("null")){
			poBreakdown.setPoId("0");
		}
		//查询所有信息或者按条件查询
		List<PoBreakdown> list=poBreakdownDao.findAll(poBreakdown);
		for (int j=0; j<list.size(); j++) {
	        String matId=list.get(j).getMatId();
	        MaterialsModel mat=matDao.selectByPrimaryKey(matId);
	        list.get(j).setMatPrice(mat.getMatPrice());
	        list.get(j).setPobName(mat.getMatName());
		}
		//查询出的信息的总行数
		Long count = poBreakdownDao.findRows(poBreakdown);
		System.out.println("总行数"+count);
		//加入MAP集合
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("code",0);
		map.put("msg","");
		map.put("count",count);
		map.put("data", list);
		System.out.println("==="+new Gson().toJson(list));
		return map;
	}

	@Override
	public List<PoBreakdown> findMatIdByPoId(String poId) {
		List<PoBreakdown> list=new ArrayList<PoBreakdown>();
		if(!poId.equals("null")){
			System.out.println("poId不为空+"+poId+"====");
			list=poBreakdownDao.findMatIdByPoId(poId);
		}
		if(poId.equals("null")){
			System.out.println("poId为空+"+poId+"====");
			list=poBreakdownDao.findMatIdByPoId("0");
		}
		return list;
	}

	@Override
	public int addOrDelete(String matIds, String poId) {
		String matId[]=matIds.split(",");
		int row=0;
		if(poId.equals("null")){
			poId="0";
			System.out.println("poId为空+"+poId+"====");
			List<PoBreakdown> list=poBreakdownDao.findMatIdByPoId(poId);
			List<PoBreakdown> listOne = new ArrayList<>();
			for(int i=0; i<matId.length; i++){
				boolean flag = false;
				for(int j=0; j<list.size(); j++){
					System.out.println("采购明细材料ID+"+list.get(j).getMatId());
					if(matId[i].equals(list.get(j).getMatId())){
						//如果相等 加入数据库的信息
						listOne.add(list.get(j));
						flag = true;
					}
				}
				if(!flag){
					//如果为false 不相等 就加入listOne集合中
					MaterialsModel mat=matDao.selectByPrimaryKey(matId[i]);
					String pobId=Tools.getRandomUUID();
					PoBreakdown pob=new PoBreakdown();
					pob.setMatId(matId[i]);
					pob.setPobName(mat.getMatName());
					pob.setPobPrice(new BigDecimal(0));
					pob.setIsva(Tools.ISVA_YES);
					pob.setPoId(poId);
					pob.setPobId(pobId);
					pob.setPobNum(0);
					pob.setPobRemark("暂无");
					listOne.add(pob);
				}
			}
			//先根据药品ID删除所有的药品材料配置
			poBreakdownDao.deleteByPoId(poId);
			for(int i=0; i<listOne.size(); i++){
				//在listOne循环加入所有的数据
				row =poBreakdownDao.insertSelective(listOne.get(i));
			}
		}
		if(!poId.equals("null")){
			System.out.println("poId不为空+"+poId+"====");
			List<PoBreakdown> list=poBreakdownDao.findMatIdByPoId(poId);
			List<PoBreakdown> listOne = new ArrayList<>();
			for(int i=0; i<matId.length; i++){
				boolean flag = false;
				for(int j=0; j<list.size(); j++){
					System.out.println("采购明细材料ID+"+list.get(j).getMatId());
					if(matId[i].equals(list.get(j).getMatId())){
						//如果相等 加入数据库的信息
						listOne.add(list.get(j));
						flag = true;
					}
				}
				if(!flag){
					//如果为false 不相等 就加入listOne集合中
					MaterialsModel mat=matDao.selectByPrimaryKey(matId[i]);
					String pobId=Tools.getRandomUUID();
					PoBreakdown pob=new PoBreakdown();
					pob.setMatId(matId[i]);
					pob.setPobName(mat.getMatName());
					pob.setPobPrice(new BigDecimal(0));
					pob.setIsva(Tools.ISVA_YES);
					pob.setPoId(poId);
					pob.setPobId(pobId);
					pob.setPobNum(0);
					pob.setPobRemark("暂无");
					listOne.add(pob);
				}
			}
			//先根据药品ID删除所有的药品材料配置
			poBreakdownDao.deleteByPoId(poId);
			for(int i=0; i<listOne.size(); i++){
				//在listOne循环加入所有的数据
				row =poBreakdownDao.insertSelective(listOne.get(i));
			}
		}
		return row;
	}

	@Override
	public int updateOrAdd(String a, String conId, String poRemark,String poSumnum,String poSumprice ) {
		GsonBuilder builder = new GsonBuilder();
		builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
		    public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
		    	 
		    	return new Date();
		    }
		});
		Gson gson = builder.create();
		//使用Type类，取得相应类型对象的class属性
		java.lang.reflect.Type listType=new TypeToken<ArrayList<PoBreakdown>>(){}.getType();//TypeToken内的泛型就是Json数据中的类型
		//使用集合接受将json转换为java对象集合后的数据
		ArrayList<PoBreakdown> list = gson.fromJson(a, listType);
		String poId=Tools.getRandomUUID();
		System.out.println("list.get(0).getPoId()"+list.get(0).getPoId());
		poDao.deleteByPrimaryKey(list.get(0).getPoId());
		//if(list.get(0).getPoId().equals("0")){
		PurchaseOrderModel po=new PurchaseOrderModel();
		po.setPoId(poId);
		po.setConId(conId);
		po.setIsva(Tools.ISVA_YES);
		Date d = new Date();
		System.out.println(d+"日期");
		po.setPoTime(d);
		po.setPoState("未审核,未采购,未提交,未质检,未入库");
		po.setPoSumprice(new BigDecimal(poSumprice));
		po.setPoSumnum(Integer.valueOf(poSumnum));
		if(!poRemark.equals("null")){
			po.setPoRemark(poRemark);
		}else{
			po.setPoRemark("暂无");
		}
		poDao.insertSelective(po);
		//}
		int row=0;
		for (PoBreakdown poBreakdown : list) {
			poBreakdownDao.deleteByPoId(poBreakdown.getPoId());
			poBreakdown.setPoId(poId);
			row=poBreakdownDao.insertSelective(poBreakdown);
		}
		return row;
	}
}
